/*
GENERATE EARNINGS AND WAGE OUTCOMES

Data 	: 
Folder 	: 
Date	: 2018-01-24

Creator		: Jonas Cederlof	(JC)
Description 	:	
Notes 		:

LATSEST UPDATE 	: 
*/

********************************************************************************
clear
set more			 off
cap   log close 		_all
log using 	"../log/A3_gen_earnings.log" 		, replace 



use 	lopnr year month lonfink date firmid nrdisp dispid work tsfilled using   	"$datapath/A2_merged_RAMS_varsel.dta",clear




{ // Annual earnings and w1	(From notifying firm)
*===============================================================================	
preserve
	*keeping observations where firmid is the notifying firm
	bys lopnr : gen notifying_firm = firmid if nrdisp==dispid
	fastmax notifying_firm, by(lopnr) name(NFid)
	keep if firmid==NFid
	
	*Collapse data (Note that mean lonfink is the same as lonfink/12)
	gcollapse (mean)  lonfink (sum) work (max)  tsfilled   ,by(lopnr year firmid)
	
	sum lonfink,d
	*Winsorize annual earnings
	*forvalues y = 1985(1)2018{
	*	replace lonfink = c1_`y' if lonfink<c1_`y' 	if year==`y'
	*	replace lonfink = c99_`y' if lonfink>c99_`y'	if year==`y'
		*replace lonfink = cond(lonfink > c99_`y',c99`y',cond(lonfink <,c1_`y', c1_`y',lonfink)) if year==`y'
	*}
	
	*Generate: Annual earnings from notifying firm
	rename lonfink 	annual_ear_atfirm
	lab var annual_ear_atfirm 	"Annual earnings from notifying firm w(0 99)"
	
	
	*Generate: Average wage from notifying firm
	gen annual_w1 	= annual_ear_atfirm/work
	lab var annual_w1 		"Annual average wage from notifying firm w(0 99)"
	
	keep lopnr firmid year annual_ear_atfirm annual_w1
	compress
	save "$datapath/annual_ear_atfirm.dta", replace
restore	
}
*	
{ // Annual earnings and w2 	(From other firm)
*===============================================================================	
preserve
	*keeping observations where firmid is NOT(!) the notifying firm
	bys lopnr : gen notifying_firm = firmid if nrdisp==dispid
	fastmax notifying_firm, by(lopnr) name(NFid)
	keep if firmid!=NFid
	
	*Collapse data (collapsing two times to get # of months worked correct)
	gcollapse (sum)  lonfink       	    (max) work 	 tsfilled  ,by(lopnr year month)
	gcollapse (mean) lonfink  (sum) work (max) 	 tsfilled  ,by(lopnr year)
	
	*Sanity check (OK!)
	tab work
	
	*Winsorize annual earnings
	*winsor2 lonfink, cut(0 99) by(year) replace
	
	*Generate: Annual earnings from notifying firm
	rename lonfink 	annual_ear_otherfirm
	lab var annual_ear_otherfirm 	"Annual earnings from other firm w(0 99)"
	
	*Generate: Average wage from notifying firm
	gen annual_w2 	= annual_ear_otherfirm/work
	lab var annual_w2 		"Annual average wage from other firm w(0 99)"
	
	keep lopnr year annual_ear_otherfirm annual_w2
	compress
	save "$datapath/annual_ear_otherfirm.dta", replace
restore
}
*	
{ // Annual earnings and w3	(From all employers)
*===============================================================================
preserve
	gcollapse (sum)  lonfink (max) work 	   tsfilled ,by(lopnr year month)
	gcollapse (mean) lonfink (sum) work (max)   tsfilled ,by(lopnr year)
	
	
	*Months worked during a year
	bys lopnr year 	: egen 	monthsworked = total(work)
	
	*Rename annual earnings variable
	rename lonfink	annual_ear
	lab var annual_ear 	"Annual earnings (summed for all employers) w(0 99)"
	
	*Annual wages
	gen 	annual_w3 = annual_ear/monthsworked
	lab var annual_w3 	"Annual wages (summed for all employers) w(0 99)"
	
	keep lopnr year annual_ear annual_w3
	compress 
	save "$datapath/annual_ear.dta",replace
restore	
}
*

{ // Monthly wages 	(w1)	(From notifying firm)
*===============================================================================
preserve
	*keeping observations where firmid is the notifying firm
	bys lopnr : gen notifying_firm = firmid if nrdisp==dispid
	fastmax notifying_firm, by(lopnr) name(NFid)
	keep if firmid==NFid
	
	*Total months worked at notifying firm during a year
	bys lopnr year 	: egen 	monthsworked = total(work)
	
	*Collapse data
	gcollapse (mean) lonfink (max) work monthsworked year  tsfilled, by(lopnr firmid date)
	
	
	*Setting wage to missing if not working at firm in a given month
	gen 	monthly_w1 = cond(work==1, lonfink/monthsworked, .)
	lab var monthly_w1 "Earnings divided by months worked at notifying firm w(0 99)" 
	
	keep 	lopnr date monthly_w1 
	compress
	save 	"$datapath/monhtly_w1.dta",replace
restore
}
*
{ // Monthly wages 	(w2)	(From other firm(s))
*===============================================================================
preserve
	*keeping observations where firmid NOT(!) the notifying firm
	bys lopnr : gen notifying_firm = firmid if nrdisp==dispid
	fastmax notifying_firm, by(lopnr) name(NFid)
	keep if firmid!=NFid
	
	
	gcollapse (sum)  lonfink      (max) work  ,by(lopnr  year date)
	
	
	*Months worked for other firms during a year
	bys lopnr year 	: egen 	monthsworked = total(work)

	*Setting wage to missing if not working at firm in a given month
	gen 	monthly_w2 = cond(work==1, lonfink/monthsworked, .)
	lab var monthly_w2 "Earnings divided by months worked at other firm w(0 99)" 
	
	
	
	keep 	lopnr date monthly_w2 
	compress
	save 	"$datapath/monhtly_w2.dta",replace
restore
}
*


log close


/*
{ // Monthly wages 	(w0) 	(From all employers) -- Summing firm avg. wages for each month 
*===============================================================================
*Description :  w0ear_imt = sum_j[ d_ijt*(y_ijt/sum_j[d_it]) ]
*		Calculate the average mothly wage from each employer and then
*		SUMS(!) these across employers for each month.
* 		NOTE: 	These can vary at the monthly level so we are able to 
*			follow up in month 1,2,3...,35,36 etc.
*		NOTE: 	This metric has the drawback of the monthly earning
*			being able to be equal to missing at time of notification.
*			This if the indiviual is registered as not working at
*			the firm the particular month she is notified.

preserve
	*Generate: 	Average monthly wage from particular employer.
	*	 	If indivual is registered as not working in a particular 
	*		month the wage is set to missing. 
	*		I.e. work==0 --> avgwage_firm==..
	bys persid firmid year 	: egen 	monthsworked = total(work)
	bys persid firmid year 	: gen  	monthly_w0 = lonfink/monthsworked 
	replace monthly_w0 = . if work==0

	*Summing across employer each month.
	gcollapse  (sum) lonfink monthly_w0  (max) work year  tsfilled ,by(lopnr date)
	
	
	*Missing values becomes zeros when having collapsed so replace to missing
	*Use if lonfink since avgwage==0 is possible (corresponds to the same obs.)
	*Should only be observations with _merge==2.
	replace monthly_w0 = . 	if 	lonfink==0 | monthly_w0==0 // the last restriction "monthly_w0==0" is due to people being registred as work==1 but lonfink==0 for that firm
	replace monthly_w0 = . 	if 	work==0	
	
	*Winsorize 
	*winsor2 monthly_w0, cut(0 99) by(year)
	
	lab var monthly_w0 	"Earnings divided by months worked all firms w(0 99)"

	keep lopnr date monthly_w0
	compress
	save "$datapath/monthly_w0.dta",replace
restore	

}
*
{ // Monthly wages 	(w1)	(From notifying firm)
*===============================================================================
preserve
	*keeping observations where firmid is the notifying firm
	bys lopnr : gen notifying_firm = firmid if nrdisp==dispid
	fastmax notifying_firm, by(lopnr) name(NFid)
	keep if firmid==NFid
	
	*Total months worked at notifying firm during a year
	bys lopnr year 	: egen 	monthsworked = total(work)
	
	*Collapse data
	gcollapse (mean) lonfink (max) work monthsworked year  tsfilled, by(lopnr firmid date)
	
	*Winsorize annual earnings
	*winsor2 lonfink, cut(0 99) by(year)
	

	*Setting wage to missing if not working at firm in a given month
	gen 	monthly_w1 = cond(work==1, lonfink/monthsworked, .)
	lab var monthly_w1 "Earnings divided by months worked at notifying firm w(0 99)" 
	
	keep 	lopnr date monthly_w1 
	compress
	save 	"$datapath/monhtly_w1.dta",replace
restore
}
*
{ // Monthly wages 	(w2)	(From other firm)
*===============================================================================
preserve
	*keeping observations where firmid NOT(!) the notifying firm
	bys lopnr : gen notifying_firm = firmid if nrdisp==dispid
	fastmax notifying_firm, by(lopnr) name(NFid)
	keep if firmid!=NFid
	
	*Variable indicating highest paying (not notifying) firm in a month & its industry code
	*NOTE: this should function as an indicator for which firm is the MAIN EMPLOYER
	*after a worker has left the notifying firm. 
	fastmax lonfink, by(lopnr date work) name(highest_annual_ear)
	replace highest_annual_ear=. if work==0
	gen firmid_other = firmid 	if lonfink==highest_annual_ear  & work==1
	gen astsni02_other = astsni02 	if firmid_other == firmid 	& work==1
	gen astsni07_other = astsni07 	if firmid_other == firmid 	& work==1
	
	*Collapse data (collapsing two times to get # of months worked correct)
	order lopnr  date eventtime_def NFid firmid lonfink year work tsfilled firmid_other astsni02_other astsni07_other
	sort lopnr date  firmid_other  // must do this sorting in order to be able to keep string (astsni) in collapse
	keep lopnr date  persid lonfink year work tsfilled firmid firmid_other astsni02_other astsni07_other // for faster collapse
	
	
	gcollapse (sum)  lonfink     (max) year work  tsfilled firmid_other (first) astsni02_other astsni07_other ,by(lopnr date)
	
	*Replace annual earnings to missing for years which we lack data (post 2013)
	replace lonfink = . 		if tsfilled==1
	replace work    = . 		if tsfilled==1
	
	*Winsorize annual earnings
	*winsor2 lonfink, cut(0 99) by(year)
	
	*Months worked for other firm during a year
	bys lopnr year 	: egen 	monthsworked = total(work)
	sum lonfink if months==0	// OK!
	
	*Setting wage to missing if not working at firm in a given month
	gen 	monthly_w2 = cond(work==1, lonfink/monthsworked, .)
	lab var monthly_w2 "Earnings divided by months worked at other firm w(0 99)" 
	
	
	
	keep 	lopnr date monthly_w2 firmid_other astsni02_other astsni07_other
	compress
	save 	"$datapath/monhtly_w2.dta",replace
restore
}
*
